package org.dairy.farms.nandana.core.logic;

import org.dairy.farms.core.NandanaThreadLocal;
import org.dairy.farms.core.UserSession;
import org.dairy.farms.dao.ExpenseTypeDAO;
import org.dairy.farms.dao.FarmDAO;
import org.dairy.farms.dao.LivestockDAO;
import org.dairy.farms.model.ExpenseNotes;
import org.dairy.farms.model.Farm;
import org.dairy.farms.model.FarmExpense;
import org.dairy.farms.model.Livestock;
import org.dairy.farms.nandana.web.view.FarmExpenseView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jun 16, 2011
 * Time: 4:10:26 PM
 */
@Service(value = "farmManagementLogic")
public class FarmManagementLogicImpl implements FarmManagementLogic {
    private static final Logger logger = LoggerFactory.getLogger(FarmManagementLogicImpl.class);
    @Autowired
    FarmDAO farmDAO;
    @Autowired
    LivestockDAO livestockDao;
    @Autowired
    private ExpenseTypeDAO expenseTypeDAO;

    public List<Farm> getAllFarms() {
        return farmDAO.loadAll();
    }

    public List<Farm> getUserFarmLivestockListCount() {
        UserSession userSession = NandanaThreadLocal.get();
        List<Farm> farmsList = farmDAO.loadUserFarms(userSession.getPersonId());
        for (Farm farm : farmsList) {
            logger.debug("Farm livestock count " + farm.getName() + " : " + farm.getLivestocks().size());
        }
        return farmsList;
    }

    public List<Livestock> getFemaleLivestockOfFarm(Long farmId) {
        return livestockDao.findLivestockOfFarmByGender(farmId, Livestock.Gender.FEMALE);
    }

    public List<Livestock> getMaleLivestockOfFarm(Long farmId) {
        return livestockDao.findLivestockOfFarmByGender(farmId, Livestock.Gender.MALE);
    }

    public long getFemaleLivestockOfFarmCount(Long farmId) {
        return -1;
    }

    public long getMaleLivestockOfFarmCount(Long farmId) {
        return -1;
    }

    public Long getLivestockOfFarmCount(Long farmId) {
        return livestockDao.findLivestockOfFarmCount(farmId);
    }

    public List<Livestock> getLivestockOfFarm(Long farmId, int page, int start, int limit) {
        return livestockDao.findLivestockOfFarm(farmId, page, start, limit);
    }

    @Override
    public Farm updateFarmExpense(FarmExpenseView farmExpenseView) {
        Farm farm = farmDAO.findById(farmExpenseView.getFarmId());
        if (farmExpenseView.getFarmExpenseId() == null) {
            FarmExpense farmExpense = new FarmExpense();
            farmExpense.setFarm(farm);
            farmExpense.setAmount(farmExpenseView.getAmount());
            farmExpense.setExpenseType(expenseTypeDAO.findById(farmExpenseView.getExpenseTypeId()));
            if(farmExpenseView.getExpenseNote()!=null && !farmExpenseView.getExpenseNote().isEmpty()){
                ExpenseNotes expenseNotes = new ExpenseNotes();
                expenseNotes.setNote(farmExpenseView.getExpenseNote());
                expenseNotes.setExpense(farmExpense);
                farmExpense.getExpenseNotes().add(expenseNotes);
            }
        }
        return farm;
    }
}
